package JAVA8.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author :李红磊
 * @version :1.0
 * @date :2021/10/8 18:04
 * @description :四大函数式接口
 */
public class Demo1 {

    //消费型函数式接口
    static void cons(float money, Consumer<Float> consumer) {
        consumer.accept(money);
    }

    ;

    //供应型函数式接口
    static List<Integer> supply(int count, Supplier<Integer> supplier) {

        ArrayList<Integer> list = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            list.add(supplier.get());

        }


        return list;
    }


    //断言型函数式接口
    static List<Student> pred(List<Student> students, Predicate<Student> predicate) {
        ArrayList<Student> list = new ArrayList<>();

        for (Student student : students) {

            if (predicate.test(student)) {
                list.add(student);
            }

        }
        return list;

    }


    //函数型函数式接口
    static String func(String name, Function<String,Integer> function){
        Integer integer = function.apply(name);
        return name+"的学号是"+integer;
    };


    public static void main(String[] args) {

        //消费型函数式接口测试
        cons(100.0f, (x) -> System.out.println("消费了" + x + "元"));

        //供应型函数式接口
        List<Integer> integerList = supply(10, () -> (int) (Math.random() * 10));
        for (Integer integer : integerList) {
            System.out.println(integer);
        }

        System.out.println("=============================");

        //断言型函数式接口测试
        List<Student> students = Arrays.asList(new Student("李宏磊", 21, 1.80f),
                new Student("于谦", 24, 1.70f),
                new Student("张居正", 15, 1.60f)
                , new Student("孙传庭", 51, 1.75f));

        List<Student> result = pred(students, (x) -> x.getAge() >= 18);

        for (Student element : result) {
            System.out.println(element);
        }

        //函数型函数式接口测试
        String func = func("李宏磊", (x) -> 142);
        System.out.println(func);

        Demo1 demo1 = new Demo1();

        //方法引用
        Consumer<String> consumer=System.out::println;
        Predicate<Integer> predicate=demo1::tes;


    }

    boolean tes(Integer integer){
        return false;
    }

}
